BINOMTEST
Overview
The BINOMTEST function performs a binomial test, a statistical hypothesis test used to determine whether the observed proportion of successes in a series of independent trials differs significantly from a hypothesized probability. This test is fundamental in quality control, clinical trials, and any scenario involving binary outcomes (success/failure, yes/no, pass/fail).
The binomial test evaluates the null hypothesis that the probability of success in a Bernoulli experiment equals a specified value p. Given k observed successes out of n trials, the test calculates a p-value representing the probability of observing a result at least as extreme as k, assuming the null hypothesis is true.
This implementation uses the scipy.stats.binomtest function from the SciPy library. The function performs a two-sided test by default, meaning it tests whether the observed proportion differs from p in either direction.
The p-value is computed exactly using the binomial distribution probability mass function:
P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}
where \binom{n}{k} is the binomial coefficient. For a two-sided test, the p-value sums probabilities of all outcomes as extreme or more extreme than the observed result.
Interpreting results: A small p-value (typically < 0.05) suggests the observed proportion differs significantly from the hypothesized probability, leading to rejection of the null hypothesis. A p-value close to 1 indicates the observed result is consistent with the null hypothesis.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=BINOMTEST(k, n, p)
k(int, required): Number of successes observed in the experiment.n(int, required): Total number of trials in the experiment.p(float, optional, default: 0.5): Hypothesized probability of success on each trial.
Returns (list[list]): 2D list [[k, p_value]], or error message string.
Examples
Example 1: Fair coin test (5 heads in 10 flips)
Inputs:
| k | n | p |
|---|---|---|
| 5 | 10 | 0.5 |
Excel formula:
=BINOMTEST(5, 10, 0.5)
Expected output:
| Result | |
|---|---|
| 5 | 1 |
Example 2: Biased coin test (8 heads in 10 flips)
Inputs:
| k | n | p |
|---|---|---|
| 8 | 10 | 0.5 |
Excel formula:
=BINOMTEST(8, 10, 0.5)
Expected output:
| Result | |
|---|---|
| 8 | 0.1094 |
Example 3: Custom probability (7 successes in 12 trials at p=0.6)
Inputs:
| k | n | p |
|---|---|---|
| 7 | 12 | 0.6 |
Excel formula:
=BINOMTEST(7, 12, 0.6)
Expected output:
| Result | |
|---|---|
| 7 | 1 |
Example 4: Default probability (3 successes in 6 trials)
Inputs:
| k | n |
|---|---|
| 3 | 6 |
Excel formula:
=BINOMTEST(3, 6)
Expected output:
| Result | |
|---|---|
| 3 | 1 |
Python Code
import math
from scipy.stats import binomtest as scipy_binomtest
def binomtest(k, n, p=0.5):
"""
Perform a binomial test for the probability of success in a Bernoulli experiment.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binomtest.html
This example function is provided as-is without any representation of accuracy.
Args:
k (int): Number of successes observed in the experiment.
n (int): Total number of trials in the experiment.
p (float, optional): Hypothesized probability of success on each trial. Default is 0.5.
Returns:
list[list]: 2D list [[k, p_value]], or error message string.
"""
try:
k_int = int(k)
n_int = int(n)
p_float = float(p)
except (TypeError, ValueError):
return "Invalid input: k and n must be integers, p must be a float."
if n_int <= 0:
return "Invalid input: n must be a positive integer."
if k_int < 0:
return "Invalid input: k must be non-negative."
if k_int > n_int:
return "Invalid input: k cannot exceed n."
if not (0 <= p_float <= 1):
return "Invalid input: p must be between 0 and 1."
result = scipy_binomtest(k_int, n_int, p_float)
pval = float(result.pvalue)
if not math.isfinite(pval):
return "Invalid output: p-value is not finite."
return [[k_int, pval]]